from flask_restful import Resource, fields, marshal_with, request
import service.report.report_service as service
import view.common as view_common
from exception.api_exception import APIException
from flask import send_file
import tempfile


class SearchData(Resource):
    """
    查询数据
    """
    res_obj = {
        'code': fields.Integer,
        'message': fields.String,
        'result': fields.Raw
    }

    @marshal_with(res_obj)
    def get(self):
        try:
            data = request.args
            record_id = int(data.get("record_id"))
        except Exception as e:
            raise APIException(message="解析参数错误" + str(e))
        res = service.search_data(record_id)
        return view_common.success(res=res)


class RecordConfig(Resource):
    """
    查询文件信息
    """
    res_obj = {
        'code': fields.Integer,
        'message': fields.String,
        'result': fields.Raw
    }

    @marshal_with(res_obj)
    def get(self):
        try:
            data = request.args
            record_id = int(data.get("record_id"))
        except Exception as e:
            raise APIException(message="解析参数错误" + str(e))
        res = service.record_config(record_id)
        return view_common.success(res=res)


class List(Resource):
    """
    查询文件信息
    """
    res_obj = {
        'code': fields.Integer,
        'message': fields.String,
        'result': fields.Raw
    }

    @marshal_with(res_obj)
    def get(self):
        try:
            data = request.args
            page_num = int(data.get("page_num"))
            page_size = int(data.get("page_size"))
        except Exception as e:
            raise APIException(message="解析参数错误" + str(e))
        res = service.record_list(page_num, page_size)
        return view_common.success(res=res)


class DeleteFile(Resource):
    """
    删除文件
    """
    res_obj = {
        'code': fields.Integer,
        'message': fields.String,
        'result': fields.Raw
    }

    @marshal_with(res_obj)
    def post(self):
        try:
            data = request.get_json()
            record_id = int(data.get("record_id"))
        except Exception as e:
            raise APIException(message="解析参数错误" + str(e))
        res = service.delete_file(record_id)
        return view_common.success(res=res)


class ModifyFileName(Resource):
    """
    修改文件名称
    """
    res_obj = {
        'code': fields.Integer,
        'message': fields.String,
        'result': fields.Raw
    }

    @marshal_with(res_obj)
    def post(self):
        try:
            data = request.get_json()
            record_id = int(data.get("record_id"))
            file_name = data.get("file_name")
        except Exception as e:
            raise APIException(message="解析参数错误" + str(e))
        res = service.modify_file_name(record_id, file_name)
        return view_common.success(res=res)


class ExportReport(Resource):
    """
    导出报告
    """
    res_obj = {
        'code': fields.Integer,
        'message': fields.String,
        'result': fields.Raw
    }

    @marshal_with(res_obj)
    def get(self):
        try:
            data = request.args
            record_id = int(data.get("record_id"))
            export_type = data.get("export_type")
        except Exception as e:
            raise APIException(message="解析参数错误" + str(e))
        temp_file, file_name = service.export_report(record_id, export_type)
        return send_file(temp_file.name, download_name=file_name, as_attachment=True)


class Analysis(Resource):
    """
    分析数据
    """

    # res_obj = {
    #     'code': fields.Integer,
    #     'message': fields.String,
    #     'result': fields.Raw
    # }
    #
    # @marshal_with(res_obj)
    def post(self):
        try:
            data = request.get_json()
            record_id = int(data.get("record_id"))
            config = data.get("config")
        except Exception as e:
            raise APIException(message="解析参数错误" + str(e))
        # return service.analysis(record_id, config)
        # return view_common.success(res=res)
        temp_file, file_name = service.analysis(record_id, config)
        return send_file(temp_file, download_name=file_name, as_attachment=True)


class TitleTid(Resource):
    """
    查询标题和tid信息
    """
    res_obj = {
        'code': fields.Integer,
        'message': fields.String,
        'result': fields.Raw
    }

    @marshal_with(res_obj)
    def post(self):
        try:
            data = request.get_json()
            record_id = int(data.get("record_id"))
        except Exception as e:
            raise APIException(message="解析参数错误" + str(e))
        res = service.title_tid(record_id)
        return view_common.success(res=res)


class DownloadData(Resource):
    """
    下载数据
    """
    res_obj = {
        'code': fields.Integer,
        'message': fields.String,
        'result': fields.Raw
    }

    # @marshal_with(res_obj)
    def get(self):
        try:
            data = request.args
            record_id = int(data.get("record_id"))
        except Exception as e:
            raise APIException(message="解析参数错误" + str(e))
        temp_file, file_name = service.download_data(record_id)
        return send_file(temp_file.name, download_name=file_name, as_attachment=True)


class UploadData(Resource):
    """
    上传数据
    """
    res_obj = {
        'code': fields.Integer,
        'message': fields.String,
        'result': fields.Raw
    }

    @marshal_with(res_obj)
    def post(self):
        if 'file' not in request.files:
            return '没有文件被上传'
        file_data = request.files['file']
        res = service.upload_data(file_data)
        return view_common.success(res=res)
